// pages/mine/mine.js
import {promisic, SplitFormatSecond} from "../../utils/util"
import {User} from "../../model/user"
import {GetUserTravels} from "../../model/travels"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showLoginBtn: false,
    info: null,
    travelsList: [],
    punch: null,
    userInfo: null,
    apply: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {

  },
  async hasAuthUserInfo() {
    const setting = await promisic(wx.getSetting)()
    const userInfo = setting.authSetting['scope.userInfo']
    return !!userInfo
  },
  async getUserInfo(e) {
    if (e.detail.userInfo) {
      const user = new User()
      await user.updateUserInfo({
        source_name: e.detail.userInfo.nickName,
        source_avatar: e.detail.userInfo.avatarUrl
      })
      this.setData({
        showLoginBtn: false
      })
    }
  },
  onRecordTap() {
    wx.navigateTo({
      url: '/pages/pkRecord/pkRecord'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: async function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function () {
    const showLogin = !await this.hasAuthUserInfo()
    const user = new User()
    const userInfo = wx.getStorageSync('userInfo')
    const apply = wx.getStorageSync('apply')
    const info = await user.getUserInformation()
    info.hour = (info.study_duration / 60 / 60).toFixed(2)
    GetUserTravels().then(res => {
      const punch = res.pop()
      this.setData({
        travelsList: res,
        punch,
        userInfo,
        apply
      })
    })
    this.setData({
      showLoginBtn: showLogin,
      info
    })
  }
})
